//获取地址栏传来的参数
var flag = 0;

// function GetQueryString(name) {
//     var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
//     var r = window.location.search.substr(1).match(reg);//search,查询？后面的参数，并匹配正则
//     if (r != null)
//         return unescape(r[2]);
//     return null;
// }

function getDate() {
    var time = new Date();
    document.getElementById("start_time").value = addDate(time,-7);
    document.getElementById("end_time").value = addDate(time,+1);
    AddProjects();
}

//加载版本号
function AddProjects() {
    $.ajax({
        url: "/asr/FindAllProjects",
        data: {
            tablename: "jira_assignees"
        },
        type: "Post",
        //async:falase,
        success: function (data) {
            if (data.code != null) {
                var code = data.code;
                var message = data.message;
                if (code === "400") {
                    alert(message);
                    window.parent.location.href = "/login.html";
                } else if (code === "401") {
                    alert(message);
                    window.location.href = "/error/error.html";
                }
                return;
            }
            var items = "";
            var item = "";
            $.each(data, function (i, result) {
                if (result !== "null"){
                    item = "<option value='" + result + "'>" + result + "</option>";
                    items += item;
                    // console.log(item, items);
                }
            });
            items += "<option selected='selected' value=''>所有项目号</option>";
            $('#project_select').html(items);
        }
    });
}

//按日期查找
function search() {
    flag = 1;
    url = "/asr/FindByProjectKeyByTime";
    document.getElementById("box_center").style.display = "block";
    document.getElementById("highchart").style.display = "block";
    document.getElementById("box_bottom2").style.display = "none";
    document.getElementById("box_bottom").style.display = "block";
    var project = document.getElementById("project_select").value;
    console.log(project);
    console.log(typeof project);
    var start = document.getElementById("start_time").value;
    var end = document.getElementById("end_time").value;
    var tablename = "jira_assignees";
    if (start === "" || end === "")
        url = "/asr/FindAllByProjectKey";
    $("#show_table").bootstrapTable('destroy');
    if (project === ""){
        //所有项目
        $('#show_table').bootstrapTable({
            url: "/asr/FindAllByTime",
            method: 'post',
            contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
            cache: false,
            async: false,
            queryParams: queryParams0,
            striped: true,
            pagination: true,
            sortable: true,
            // sortOrder: "asc",//升序
            sortOrder: "desc",//降序
            sortName: 'staff_id',
            pageSize: 10,
            pageNumber: 1,
            pageList: [10, 20, 50, 100, 200, 500],
            search: true,
            strictSearch: false,
            showColumns: true,
            showRefresh: false,
            showExport: false,
            uniqueId: "id",
            showToggle: false,
            cardView: false,
            // clickToSelect: true,
            showFooter: true,

            columns:
                [
                    // {field: "checked", checkbox: true},
                    {field: "staffName", title: "姓名", align: "center", valign: "middle", sortable: "true"},
                    {field: "projectKey", title: "项目号", align: "center", valign: "middle", sortable: "true"},
                    {field: "insertDate", title: "日期", align: "center", valign: "middle", sortable: "true"},

                    {field: "todayNewBug", title: "新增", align: "center", valign: "middle", sortable: "true"},
                    {field: "solved", title: "已解决", align: "center", valign: "middle", sortable: "true"},
                    {field: "open", title: "开放", align: "center", valign: "middle", sortable: "true"},
                    {field: "inProgress", title: "处理中", align: "center", valign: "middle", sortable: "true"},
                    {field: "reopen", title: "重打开", align: "center", valign: "middle", sortable: "true"},
                    {field: "closed", title: "已关闭", align: "center", valign: "middle", sortable: "true"},

                    {field: "priorityHighest", title: "紧急", align: "center", valign: "middle", sortable: "true"},
                    {field: "priorityHigh", title: "优先", align: "center", valign: "middle", sortable: "true"},
                    {field: "priorityMedium", title: "中等", align: "center", valign: "middle", sortable: "true"},
                    {field: "priorityLow", title: "滞后", align: "center", valign: "middle", sortable: "true"},
                    {field: "priorityLowest", title: "不急", align: "center", valign: "middle", sortable: "true"},
                ],
        });
    } else{
        $('#show_table').bootstrapTable({
            url: url,
            method: 'post',
            contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
            cache: false,
            async: false,
            queryParams: queryParams,
            striped: true,
            pagination: true,
            sortable: true,
            // sortOrder: "asc",//升序
            sortOrder: "desc",//降序
            sortName: 'staff_id',
            pageSize: 10,
            pageNumber: 1,
            pageList: [10, 20, 50, 100, 200, 500],
            search: true,
            strictSearch: false,
            showColumns: true,
            showRefresh: false,
            showExport: false,
            uniqueId: "id",
            showToggle: false,
            cardView: false,
            // clickToSelect: true,
            showFooter: true,

            columns:
                [
                    // {field: "checked", checkbox: true},
                    {field: "staffName", title: "姓名", align: "center", valign: "middle", sortable: "true"},
                    {field: "insertDate", title: "日期", align: "center", valign: "middle", sortable: "true"},

                    {field: "todayNewBug", title: "新增", align: "center", valign: "middle", sortable: "true"},
                    {field: "solved", title: "已解决", align: "center", valign: "middle", sortable: "true"},
                    {field: "open", title: "开放", align: "center", valign: "middle", sortable: "true"},
                    {field: "inProgress", title: "处理中", align: "center", valign: "middle", sortable: "true"},
                    {field: "reopen", title: "重打开", align: "center", valign: "middle", sortable: "true"},
                    {field: "closed", title: "已关闭", align: "center", valign: "middle", sortable: "true"},

                    {field: "priorityHighest", title: "紧急", align: "center", valign: "middle", sortable: "true"},
                    {field: "priorityHigh", title: "优先", align: "center", valign: "middle", sortable: "true"},
                    {field: "priorityMedium", title: "中等", align: "center", valign: "middle", sortable: "true"},
                    {field: "priorityLow", title: "滞后", align: "center", valign: "middle", sortable: "true"},
                    {field: "priorityLowest", title: "不急", align: "center", valign: "middle", sortable: "true"},
                ],
        });
    }


    function queryParams(params) {
        return {
            tablename: tablename,
            projectkey: project,
            start: start,
            end: end
        }
    }

    function queryParams0(params) {
        return {
            tablename: tablename,
            start: start,
            end: end
        }
    }

    Highcharts.chart('highchart', {
        chart: {
            type: 'column'
        },
        title: {
            text: '员工指标'
        },
        subtitle: {
            text: '暂无有效数据'
        },
        xAxis: {
            categories: [
                '甲', '乙', '丙', '丁'
            ],
            crosshair: true
        },
        yAxis: {
            min: 0,
            title: {
                text: '数量（个）'
            }
        },
        tooltip: {
            // head + 每个 point + footer 拼接成完整的 table
            headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
            pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
                '<td style="padding:0"><b>{point.y} 个</b></td></tr>',
            footerFormat: '</table>',
            shared: true,
            useHTML: true
        },
        plotOptions: {
            column: {
                dataLabels: {
                    enabled: true,
                },
                borderWidth: 0
            }
        },
        series: [{
            name: '新增bug',
            data: [5, 6, 1, 3]
        }, {
            name: '解决bug',
            data: [5, 2, 8, 4]
        }, {
            name: '开放',
            data: [2, 1, 4, 3]
        }, {
            name: '进行中',
            data: [4, 5, 2, 1]
        }, {
            name: '重新打开',
            data: [4, 2, 1, 6]
        }, {
            name: '已关闭',
            data: [3, 4, 1, 2]
        }]
    });
}

//返回上一级
function returnLastPage() {
    switch (flag) {
        case 0:
            break;
        case 1:
            break;
        case 2:
            search();
            break;
    }
}